<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>swap</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Vectors</a> &gt; <a href="swap.html">swap</a>
  </div>

  <div class="name-format">
    swap
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;vector&gt;
  void swap( container&amp; from );
</pre>

  <p>The swap() function exchanges the elements of the current vector
  with those of <em>from</em>. This function operates in <a href=
  "../complexity.html">constant time</a>.</p>

  <p>For example, the following code uses the swap() function to
  exchange the contents of two vectors:</p>
  <pre class="example-code">
  vector<string> v1;
  v1.push_back("I'm in v1!");

  vector<string> v2;
  v2.push_back("And I'm in v2!");

  v1.swap(v2);

  cout << "The first element in v1 is " << v1.front() << endl;
  cout << "The first element in v2 is " << v2.front() << endl;
</pre>

  <p>The above code displays:</p>
  <pre class="example-code">
  The first element in v1 is And I'm in v2!
  The first element in v2 is I'm in v1!
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="vector_operators.html">= operator</a><br>
    (C++ Lists) <a href="../cpplist/splice.html">splice</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
